//
//  MineTableViewController.swift
//  IDF
//
//  Created by 陈徐挺 on 26/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit
import EasyPeasy
import YYCache
import SafariServices

class MineTableViewController: UITableViewController {
    @IBOutlet weak var phoneLabel: UILabel!
    var helpUrl: String = ""
    var aboutUrl: String = ""
    var contactusUrl: String = ""
    
    let signOutBtn = UIButton(frame: .zero)
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        if !appLogined {
            showLoginView()
            mainTabBarController.selectedIndex = 0
            nowNavgationController = mainTabBarController.viewControllers![0] as! UINavigationController
        } else {
             phoneLabel.text = appUser.phone
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Mine"
        tableView.tableFooterView = UIView()
        tableView.addSubview(signOutBtn)
        signOutBtn <- [
            Left(15).to(tableView, .left),
            Width(kWidth - 30),
            Height(50),
            Top(280).to(tableView, .top)
        ]
        signOutBtn.setTitle("Sign out", for: .normal)
        signOutBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        signOutBtn.setTitleColor(UIColor(hexString: "#e02e44"), for: .normal)
        signOutBtn.backgroundColor = .white
        signOutBtn.layer.borderWidth = 1
        signOutBtn.layer.borderColor = UIColor(hexString: "#dddddd")?.cgColor
        signOutBtn.layer.cornerRadius = 2
        
        signOutBtn.setBlockFor(.touchUpInside) { (button) in
            appUser = User()
            let cache = YYCache(name: "appcache")
            cache?.removeAllObjects()
            appLogined = false
            
            mainTabBarController.selectedIndex = 0
            nowNavgationController = mainTabBarController.viewControllers![0] as! UINavigationController
        }
        helpUrl = appConfig.helpUrl!
        aboutUrl = appConfig.aboutUrl!
        contactusUrl = appConfig.contactusUrl!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
         tableView.deselectRow(at: indexPath, animated: true)
        
        var stringUrl = ""
        var title = ""
        switch indexPath.row {
        case 0:
            stringUrl = contactusUrl
            title = "Contact us"
        case 1:
            stringUrl = aboutUrl
            title = "About"
        case 2:
            stringUrl = helpUrl
            title = "Help"
        default:
            stringUrl = "https://www.baidu.com"
        }
        let url = URL(string: stringUrl)
        let next: WebViewController = initViewContollerWithStoryboard(storyboardName: "Mine", storyboardId: "webViewController")
        next.navigationItem.title = title
        next.urlRequest = URLRequest(url: url!)
        navigationController?.pushViewController(next, animated: true)

    }
    
}
